import Day from 'dayjs';
import BaseModel from './base_model';

/**
 * 团队成员领域模型
 * 封装团队成员业务逻辑与规则
 */
class TeamMemberModel extends BaseModel {
  constructor(data = {}) {
    super(data);
    // 基本属性
    this._id = data._id || '';
    this.userId = data.userId || '';
    this.projectId = data.projectId || '';
    this.userName = data.userName || '';
    this.userAvatar = data.userAvatar || '';
    this.joinTime = data.joinTime || Day().valueOf();
    this.role = data.role || 'member'; // 角色：leader, manager, member
    this.departmentId = data.departmentId || '';
    this.departmentName = data.departmentName || '';
    this.responsibilities = data.responsibilities || [];
    this.permissions = data.permissions || this.getDefaultPermissions(data.role);
    this.workload = data.workload || 0; // 工作量分配（百分比）
    this.status = data.status || 'active'; // 状态：active, inactive
    this.tasks = data.tasks || []; // 关联的任务ID列表
    
    // 计算属性
    this.isActive = this.status === 'active';
    this.isLeader = this.role === 'leader';
    this.isManager = this.role === 'manager';
  }
  
  /**
   * 获取角色默认权限
   * @param {String} role 角色类型
   * @returns {Object} 权限配置
   */
  getDefaultPermissions(role) {
    switch(role) {
      case 'leader':
        return {
          project: { read: true, write: true, delete: true, admin: true },
          task: { read: true, write: true, delete: true, assign: true },
          document: { read: true, write: true, delete: true, approve: true },
          member: { read: true, write: true, delete: true, assign: true }
        };
      case 'manager':
        return {
          project: { read: true, write: true, delete: false, admin: false },
          task: { read: true, write: true, delete: true, assign: true },
          document: { read: true, write: true, delete: true, approve: false },
          member: { read: true, write: false, delete: false, assign: true }
        };
      case 'member':
      default:
        return {
          project: { read: true, write: false, delete: false, admin: false },
          task: { read: true, write: true, delete: false, assign: false },
          document: { read: true, write: true, delete: false, approve: false },
          member: { read: true, write: false, delete: false, assign: false }
        };
    }
  }
  
  /**
   * 检查是否有特定权限
   * @param {String} module 模块：project, task, document, member
   * @param {String} operation 操作：read, write, delete, admin, assign, approve
   * @returns {Boolean} 是否有权限
   */
  hasPermission(module, operation) {
    return this.permissions && 
           this.permissions[module] && 
           this.permissions[module][operation] === true;
  }
  
  /**
   * 转换为存储格式
   */
  toStorage() {
    const data = {...this};
    
    // 确保日期格式一致
    if (data.joinTime && typeof data.joinTime === 'string') {
      data.joinTime = Day(data.joinTime).valueOf();
    }
    
    return data;
  }
  
  /**
   * 获取角色显示名称
   */
  getRoleDisplay() {
    const roleMap = {
      'leader': '项目负责人',
      'manager': '项目经理',
      'member': '团队成员'
    };
    return roleMap[this.role] || '团队成员';
  }
  
  /**
   * 验证团队成员数据是否有效
   */
  validate() {
    const errors = {};
    
    if (!this.userId) errors.userId = '用户ID不能为空';
    if (!this.projectId) errors.projectId = '项目ID不能为空';
    if (!this.userName) errors.userName = '用户名称不能为空';
    if (!this.role) errors.role = '角色不能为空';
    
    return {
      valid: Object.keys(errors).length === 0,
      errors
    };
  }
}

export default TeamMemberModel; 